package com.example.dell.sprinnertext_zzy122;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
 private TextView tvinfo;
    private Spinner spinner;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        //定义数据源
        String[] cities={"北京","上海","大连"};
        //定义适配器
        final ArrayAdapter<String> adapter=new ArrayAdapter<String>(
                this,//上下文
                android.R.layout.simple_spinner_item,
                cities//数据源

        );
        adapter.setDropDownViewResource(
                android.R.layout.simple_spinner_dropdown_item);
        //Spinner与Adapter绑定---4
        spinner.setAdapter(adapter);
        //点击条事件----5
        spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                tvinfo.setText("您选择的是："
                +adapter.getItem(position).toString());
            }
            public void onNothingSelected(AdapterView<?> adapterView){
                //不操作
            }
        });
    }

    private void init() {//获取控件
        tvinfo=(TextView)findViewById(R.id.textView);
        spinner=(Spinner)findViewById(R.id.spinner);

    }
}
